package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.SysApplication;
import com.tepth.maintenancesystem.api.MaintainHttp;
import com.tepth.maintenancesystem.bean.Contents;
import com.tepth.maintenancesystem.bean.MaintainListWork;
import com.tepth.maintenancesystem.bean.VehicleMaintain;
import com.tepth.maintenancesystem.adapter.MaintainCompletedLeftAdapter;
import com.tepth.maintenancesystem.adapter.MaintainDetailsCompletedRightAdapter;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class MaintainCompletedDoneActivity extends AppCompatActivity {

    private MaintainCompletedLeftAdapter leftAdapter;
    private MaintainDetailsCompletedRightAdapter rightAdapter;

    private ListView leftListView;
    private ListView rightListView;

    private Context context;

    private ArrayList<MaintainListWork> listCompleted = new ArrayList<>();
    //    private List<Contents> doingCompleted = new ArrayList<>();//竣检的集合
    private VehicleMaintain obj;
    private LoadingDialog loadingDialog;
    private RelativeLayout rl_completed_back;
    private TextView tv_completed_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_done_completed);
        SysApplication.getInstance().addActivity(this);
        context = this;
        Bundle bundle = this.getIntent().getBundleExtra("MaintainBundle");
        obj = (VehicleMaintain) bundle.getSerializable("Maintain");

        initView();
        getListWorkContentsNormal();
    }

    private void initView() {
        leftListView = (ListView) findViewById(R.id.left_completed_listview);
        rightListView = (ListView) findViewById(R.id.right_completed_listview);
        tv_completed_title = (TextView) findViewById(R.id.tv_completed_title);
        rl_completed_back = (RelativeLayout) findViewById(R.id.rl_completed_back);
        tv_completed_title.setText(obj.getVehicleNumber() + "  竣工检验");
        rl_completed_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initLeftListView() {
        leftAdapter = new MaintainCompletedLeftAdapter(this, listCompleted);
        leftAdapter.setSelectPosition(0);
        leftListView.setAdapter(leftAdapter);
        initRightListView(listCompleted.get(0).getListContents());

        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                updataRightListView(position);
            }
        });
    }

    private void updataRightListView(int position) {
        leftAdapter.setSelectPosition(position);
        leftAdapter.notifyDataSetChanged();
        //更新第二ListView
        rightAdapter.setDatas(listCompleted.get(position).getListContents());
        rightAdapter.notifyDataSetChanged();
    }

    private void initRightListView(List<Contents> listContents) {
        rightAdapter = new MaintainDetailsCompletedRightAdapter(this, listContents);
        rightListView.setAdapter(rightAdapter);
        rightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Contents object = (Contents) rightAdapter.getItem(position);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("contents", object);
                bundle.putString("vehicleName", obj.getVehicleNumber());
                intent.putExtra("bundle", bundle);
                intent.setClass(context, MaintainWorkCompletedDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getListWorkContentsNormal() {
        loadingDialog = new LoadingDialog(context);
        loadingDialog.setInterceptBack(false)
                .setLoadingText("正在加载...");
        loadingDialog.show();
        new Thread() {
            @Override
            public void run() {
                listCompleted = MaintainHttp.getListCompleted(context, obj.getId());
                myHandler.sendEmptyMessage(0);
                super.run();
            }
        }.start();
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    loadingDialog.close();
                    if (listCompleted != null) {
                        initLeftListView();
                    } else {
                        ToastUtils.showToast(context, "网络错误");
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };
//    @Override
//    public void textOnClick(Contents object) {
//        String workName="";
//        for (int i=0;i<listCompleted.size();i++){
//            if(object.getContentTypeCode().equals(listCompleted.get(i).getContentTypeCode())){
//                workName=listCompleted.get(i).getContentTypeName();
//            }
//        }
//        Intent intent=new Intent();
//        Bundle bundle=new Bundle();
//        bundle.putParcelable("contents",object);
//        bundle.putString("workName",workName);
//        bundle.putString("vehicleName",obj.getVehicleNumber());
//        intent.putExtra("bundle",bundle);
//        if(object.getWorkPersonName().equals("null")||object.getWorkPersonName().equals("")){
//            intent.setClass(context,MaintainWorkDetailsActivity.class);
//            startActivityForResult(intent,1004);
//        }else{
//            intent.setClass(context,MaintainWorkCompletedDetailsActivity.class);
//            startActivity(intent);
//        }
//    }
}
